package org.csu.controller;

import org.csu.domain.Account;
import org.csu.domain.Signon;
import org.csu.service.IAccountService;
import org.csu.service.ISignonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author YinBo
 * @since 2025-03-08
 */
@RestController
@RequestMapping("/account")
public class AccountController {
    @Autowired
    private IAccountService accountService;

    @Autowired
    private ISignonService signonService;

    //1.更新账户
    @PutMapping("/update")
    public Result updateAccount(@RequestBody Account account) {
        System.out.println(account);
        boolean flag=accountService.updateAccount(account);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //2.查询所有
    @GetMapping("/Allusers")
    public Result getAccounts() {
        List<Account> accountList = accountService.getAccounts();
        Integer code = accountList != null ? Code.GET_OK : Code.GET_ERR;
        String msg = accountList != null ? "" : "数据查询失败，请重试！";
        return new Result(code,accountList,msg);
    }

    //3.删除账户
    @DeleteMapping("/{userid}")
    public Result deleteAccount(@PathVariable String userid) {
        boolean flag=accountService.deleteAccount(userid);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //4.重置密码
    @PutMapping("/reset-password")
    public Result resetPassword(@RequestBody Signon signon) {
        signon.setPassword("1234");
        boolean flag=accountService.resetPassword(signon);
        Integer code = flag ? Code.UPDATE_OK:Code.UPDATE_ERR;
        String msg= flag ? "重置成功":"重置失败";
        return new Result(code,msg);
    }
}
